package com.jlastu.znb.view;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.jlastu.znb.R;
import com.jlastu.znb.adapter.GoodsListAdapter;
import com.jlastu.znb.base.BaseFragment;
import com.jlastu.znb.contract.MainContract;
import com.jlastu.znb.entity.Goods;
import com.jlastu.znb.entity.Photo;
import com.jlastu.znb.presenter.BuyerHomePresenter;

import java.util.ArrayList;
import java.util.List;

public class BuyerHomeFragment extends BaseFragment implements MainContract.IMainView ,View.OnClickListener{

   private RecyclerView mGoodsRecyclerView;
   private GoodsListAdapter mRecyclerAdapter;
   private MainContract.IMainPresenter mMainPresenter;
   private List<Goods> mGoodsList;
   private List<Photo> mPhotoList;
   private LinearLayout searchLayout;
   private LinearLayout gosellerLayout;
   private TextView mTextView1;
   private TextView mTextView2;
   private TextView mTextView3;
   private TextView mTextView4;

   @Override
   protected int getLayoutId() {
      return R.layout.fragment_buyer_home;
   }

   @Override
   protected void initView() {
      mMainPresenter = new BuyerHomePresenter(this, getContext());
      mGoodsList = new ArrayList<>();
      mPhotoList = new ArrayList<>();
      mGoodsRecyclerView = mView.findViewById(R.id.rv_goods);
      StaggeredGridLayoutManager staggeredGridLayoutManager =
              new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
      mGoodsRecyclerView.setLayoutManager(staggeredGridLayoutManager);
      mRecyclerAdapter = new GoodsListAdapter(mGoodsList);
      mGoodsRecyclerView.setAdapter(mRecyclerAdapter);

      searchLayout = mView.findViewById(R.id.search_layout); // 根据实际ID修改
      searchLayout.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            // 当点击搜索框时，跳转到SearchActivity
            Intent intent = new Intent(getContext(), SearchActivity.class);
            startActivity(intent);
         }
      });
      gosellerLayout = mView.findViewById(R.id.go_seller_layout);
      gosellerLayout.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Intent intent = new Intent(getContext(), SecondActivity.class);
            startActivity(intent);
         }
      });
      mTextView1 = mView.findViewById(R.id.toolbar_title1);
      mTextView1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            // 切换状态
            setTabSelected(mTextView1);
            mMainPresenter.getGoodsList();
         }
      });
      mTextView2 = mView.findViewById(R.id.toolbar_title2);
      mTextView2.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            setTabSelected(mTextView2);
            mMainPresenter.getGoodsByCategory("水果蔬菜");
         }
      });
      mTextView3 = mView.findViewById(R.id.toolbar_title3);
      mTextView3.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            setTabSelected(mTextView3);
            mMainPresenter.getGoodsByCategory("米面粮油");
         }
      });
      mTextView4 = mView.findViewById(R.id.toolbar_title4);
      mTextView4.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            setTabSelected(mTextView4);
            mMainPresenter.getGoodsByCategory("农资农具");
         }
      });

   }

   @Override
   public void onResume() {
      super.onResume();
      mMainPresenter.getGoodsList(); // 获取全部商品列表
      mMainPresenter.getGoodsByCategory("水果蔬菜");
      mMainPresenter.getGoodsByCategory("米面粮油");
      mMainPresenter.getGoodsByCategory("农资农具");
   }

   @Override
   protected void initListener() {


   }


   private void setTabSelected(TextView selectedTab) {
      // 重置所有标签为未选中状态
      mTextView1.setSelected(false);
      mTextView2.setSelected(false);
      mTextView3.setSelected(false);
      mTextView4.setSelected(false);
      // 将被点击的标签设置为选中状态
      selectedTab.setSelected(true);
   }

   @Override
   public void showGoodsList(List<Goods> goodsList) {
      mGoodsList.clear();
      mGoodsList.addAll(goodsList);
      Log.d("BuyerHomeFragment", "showGoodsList size: " + mGoodsList.size());
      mRecyclerAdapter.notifyDataSetChanged(); // 刷新 RecyclerView 数据
   }

   @Override
   public void showGoodsByCategory(List<Goods> goodsList) {
      //if (goodsList != null && !goodsList.isEmpty()) {
         mGoodsList.clear();
         mGoodsList.addAll(goodsList);
         mRecyclerAdapter.notifyDataSetChanged(); // 刷新RecyclerView数据
      //}
   }

   @Override
   public void onClick(View v) {


   }
   public void showGoodsList(List<Goods> goodsList, List<Photo> photoList) {
      mGoodsList = goodsList;
      mPhotoList = photoList;
      Log.d("", "showGoodsList " + mGoodsList.size());
      mRecyclerAdapter.updateData(mGoodsList);
   }
}
